import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [{
        path: '/',
        component: () =>
            import ('../views/Manage.vue'),
        redirect: "/login",
        children: [{
                path: '/home',
                name: '首页',
                component: () =>
                    import ('../views/Home.vue')
            },
            {
                path: '/user',
                name: '用户管理',
                component: () =>
                    import ('../views/User.vue')
            },
            {
                path: '/admin',
                name: '管理员管理',
                component: () =>
                    import ('../views/Admin.vue')
            },
            {
                path: '/file',
                name: '文件管理',
                component: () =>
                    import ('../views/File.vue')
            },
            {
                path: '/commodity',
                name: '商品管理',

                component: () =>
                    import ('../views/Commodity.vue')

            },
            {
                path: '/person',
                name: '/个人中心',
                component: () =>
                    import ('../views/Person.vue')
            },
            {
                path: '/404',
                name: '404',
                component: () =>
                    import ('../views/404.vue')
            },
            {
                path: '/feedback',
                name: '评论管理',
                component: () =>
                    import ('../views/Feedback.vue')
            }
        ]
    },

    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('../views/Register.vue')
    },


]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name) // 设置当前的路由名称，为了在Header组件中去使用
    store.commit("setPath") // 触发store的数据更新
    next() // 放行路由
})

export default router